package com.karashi;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import net.gimite.jatts.JapaneseTextToSpeech;
import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.karashi.twitter.TimelineComparator;
import com.karashi.twitter.TwitterArrayAdapter;
import com.karashi.twitter.TwitterStatus;
import com.karashi.twitter.parser.TwitterParser;
import com.karashi.twitter.task.TweetDownloadTask;

public class MainActivity extends Activity {

	private static final int MENU_RFESH = 0;
	private static final String PUBLIC_TIMELINE_XML = "http://api.twitter.com/statuses/public_timeline.xml";

	private ArrayList<TwitterStatus> list;

	private TwitterArrayAdapter mTwitterArrayAdapter;

	private ListView lv;

	private String lang;

	private JapaneseTextToSpeech jtts;
	private HashMap<String, String> jttsParams;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		jtts = new JapaneseTextToSpeech(this, null);
		setJttsParams();

		Button button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				refleshTwitterTimeLine();
			}
		});
		
		initGetTwitterTimeline();

		lv = (ListView) findViewById(R.id.listView1);
		mTwitterArrayAdapter = new TwitterArrayAdapter(this, R.layout.twitter_row, list);
		lv.setAdapter(mTwitterArrayAdapter);
		lv.setTextFilterEnabled(true);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				TextView text = (TextView) view.findViewById(R.id.bottomtext);
				Toast.makeText(getApplicationContext(), text.getText(), Toast.LENGTH_LONG).show();

				if (!jtts.isSpeaking()) {

					jtts.speak((String) text.getText(), TextToSpeech.QUEUE_FLUSH, jttsParams);
				}
			}
		});
	}

	private void initGetTwitterTimeline() {
		lang = getResources().getConfiguration().locale.getLanguage();

		TwitterParser parser = new TwitterParser(PUBLIC_TIMELINE_XML, lang);
		ArrayList<TwitterStatus> currentList = parser.parse();
		if (list == null) {
			list = currentList;
		} else {
			list.addAll(currentList);
		}

		Collections.sort(list, new TimelineComparator());
	}

	private void setJttsParams() {
		jttsParams = new HashMap<String, String>();

		// 適応する再生音量を指定
		jttsParams.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_MUSIC));
		// utterance idを設定。
		jttsParams.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "1");

		// 声色を変える。JapaneseTextToSpeech独自オプション。
		// "male01", "female01", "male02"の3種類。
		jttsParams.put(JapaneseTextToSpeech.KEY_PARAM_SPEAKER, "female01");
	}

	private void refleshTwitterTimeLine() {

		TweetDownloadTask task = new TweetDownloadTask(mTwitterArrayAdapter, list, lang, this);

		task.execute(PUBLIC_TIMELINE_XML);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		menu.add(0, MENU_RFESH, Menu.NONE, getResources().getString(R.string.option_menu_reflesh)).setIcon(
				android.R.drawable.ic_menu_rotate);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case MENU_RFESH:

			refleshTwitterTimeLine();

			break;
		}

		return false;
	}

}